#include "stm32f10x.h"                  // Device header
#include "serial.h"
#include "adf4351.h"
#include "delay.h"
#include "timer.h"
#include "phase.h"
#include "gpio.h"
#include "iwdg.h"
#include "nvic.h"
#include "att.h"

#define PA_ON  GPIO_ResetBits(GPIOA, GPIO_Pin_11)
#define PA_OFF GPIO_SetBits(GPIOA, GPIO_Pin_11)
#define PD_ON  GPIO_SetBits(GPIOA, GPIO_Pin_12)
#define PD_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_12)

//锁相环状态
uint8_t lock_state = 0;

void SerialComand(uint8_t RxFlag)
{
    if(RxFlag)
    {
        uint8_t i,j;
        if(Serial.msgtype)
        {
            switch(Serial.msgtype)
            {
                case 1: //写ADF寄存器
                    j = Serial.payload[0];
        
                    for(i=0; i<Serial.payload[0]; i++,j--)
                        ADF.buffer[j-1] = Serial.payload[i+1];
                
                    ADF4351_Write(ADF.payload);
                    Serial_SendString("ok");
                    break;
                case 2: //设置ADF频率
                    ADF.buffer[1] = Serial.payload[1];
                    ADF.buffer[0] = Serial.payload[2];
                    
                    ADF4351_Freq(ADF.payload);
                    Serial_SendString("ok");
                    break;
                case 3: //设置ADF功率
                    ADF4351_Power(Serial.payload[1]);
                    Serial_SendString("ok");
                    break;
                case 4: //开关PA 
                    if(Serial.payload[1])
                    {
                        PA_ON;
                        Serial_SendString("PA ON");
                    }
                    else
                    {
                        PA_OFF;
                        Serial_SendString("PA OFF");
                    }
                    break;
                case 5:
                    ATT_PinSet(Serial.payload[1]);
                    Serial_SendString("att ok");
                    break;
                case 6:
                    for(uint8_t i=0; i<Serial.payload[0]; i++)
                        ATT.buffer[i] = Serial.payload[i+1];
                    Serial_SendNumber(ATT_RatioSet(ATT.payload), 2);
                    break;
                case 7:
                    //Phase_AddSub(Serial.payload[1]);
                    if(Serial.payload[1])
                    {
                        PD_ON;
                        Serial_SendString("PD ON");
                    }
                    else
                    {
                        PD_OFF;
                        Serial_SendString("PD OFF");
                    }
                    break;
//                default:
//                    Serial_SendString("err");
//                    break;
            }
        } else {
            Serial_SendString("err");
        }
    }
    if(ADF_MUX_DIGITAL != lock_state)
    {
        lock_state = ADF_MUX_DIGITAL;
        if(lock_state)
            Serial_SendString("lock");
        else 
            Serial_SendString("unlock");
    }
}

int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 
    GPIO_InitConfig(GPIOA, GPIO_Pin_11, GPIO_Mode_Out_OD, GPIO_Speed_50MHz);
    GPIO_InitConfig(GPIOA, GPIO_Pin_12, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
    Serial_Init(115200);
    ADF4351_Init();
    ATT_Init();
    delay_ms(500);
    //看门狗初始化
    IWDG_Init(10);
    
    while(1)
    {
        SerialComand(Serial_GetRxFlag());
        IWDG_ReloadCounter();   //喂狗
    }
}
